#pragma once
#include <iostream>
#include "TcpServer.hpp"
#include "Protocol.hpp"

enum
{
    DIV_ZERO = 1,
    MOD_ZERO,
    OTHER_Oper

};

class ServerCal
{
public:
    ServerCal()
    {}

    Response CalculatorHelper(const Request& req)
    {
        Response resp(0,0);
        switch (req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;
        case '-':
            resp.result = req.x - req.y;
            break;
        case '*':
            resp.result = req.x * req.y;
            break;
        case '/':
            if (req.y == 0)
            {
                resp.code = DIV_ZERO;
            }
            else
            {
                resp.result = req.x / req.y;
            }
           
            break;
        case '%':
            if (req.y == 0)
            {
                resp.code = MOD_ZERO;
            }
            else
            {
                resp.result = req.x % req.y;
            }
        
            break;
        
        default:
            resp.code = OTHER_Oper;
            break;
        }

        return resp;
    }

    // Response CalculatorHelper(const Request &req)
    // {
    //     Response resp(0, 0);
    //     switch (req.op)
    //     {
    //     case '+':
    //         resp.result = req.x + req.y;
    //         break;
    //     case '-':
    //         resp.result = req.x - req.y;
    //         break;
    //     case '*':
    //         resp.result = req.x * req.y;
    //         break;
    //     case '/':
    //     {
    //         if (req.y == 0)
    //             resp.code = DIV_ZERO;
    //         else
    //             resp.result = req.x / req.y;
    //     }
    //     break;
    //     case '%':
    //     {
    //         if (req.y == 0)
    //             resp.code = MOD_ZERO;
    //         else
    //             resp.result = req.x % req.y;
    //     }
    //     break;
    //     default:
    //         resp.code = OTHER_Oper;
    //         break;
    //     }

    //     return resp;
    // }

    std::string Calcluator(std::string &package)
    {
        std::string content;
        bool r = Decode(package,&content);

        if (!r) return "";

        Request req;
        r = req.Deserialize(content);
        if (!r) return "";

        content = "";
        Response resp = CalculatorHelper(req);
        resp.Serialize(&content);
        content = Encode(content);
        
        return content;
    }

};